//
//  GYJournalViewController.m
//  LockManagement
//
//  Created by apple on 2018/7/21.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYJournalViewController.h"
#import "GYDateSelectView.h"
#import "GYJournalTableViewCell.h"
#import "GYJournal.h"
#import "GYDateTools.h"


@interface GYJournalViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 日期选择器 */
@property (nonatomic, strong) GYDateSelectView *dateSelectView;

/** journal表格 */
@property (nonatomic, strong) UITableView *journalTableView;

/** 日子数组 */
@property (nonatomic, strong) NSMutableArray *journalists;

/** 加载当前的页数 */
@property (nonatomic, assign) int page;

/** 当前时间字符串 */
@property (nonatomic, strong) NSString *currentDateString;

/** 数据总条数 */
@property (nonatomic, assign) NSInteger totalItems;

@end

@implementation GYJournalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.dateSelectView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    self.journalTableView.frame = CGRectMake(0, 64, ScreenWidth, self.view.height-64);
}

- (void)setupUI {
    self.view.backgroundColor = RGBColor(247, 247, 247);
    [self.view addSubview:self.journalTableView];
    
    self.page = 0;
    self.totalItems = 99;
    //添加时间选择器
    GYDateSelectView *dateSelectView = [[[NSBundle mainBundle] loadNibNamed:@"GYDateSelectView" owner:nil options:nil] firstObject];
    kWeakSelf(self)
    dateSelectView.changedTimeUpdateLogCallBack = ^(NSString *dateString) {
        weakself.currentDateString = dateString;
        [weakself.journalists removeAllObjects];
        weakself.page = 0;
        weakself.totalItems = 99;

        [weakself loadMoreData];
    };
    [self.view addSubview:dateSelectView];
    self.dateSelectView = dateSelectView;
    
    self.journalTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //加载日志数据
    self.currentDateString = [GYDateTools returnCurrentDateToString:@"yyyy-MM-dd"];
    [self loadMoreData];
}

- (void)loadMoreData {
    if (self.journalists.count < self.totalItems) {
        self.page += 1;
        //1.得到当前时间
        NSString *begin = [GYDateTools dateString:self.currentDateString withDateFormatter:@"yyyy-MM-dd" hour:0 minute:0 second:0];
        NSString *end = [GYDateTools dateString:self.currentDateString withDateFormatter:@"yyyy-MM-dd" hour:23 minute:59 second:59];
        //NSLog(@"开始时间======%@",[GYDateTools dateString:self.currentDateString withDateFormatter:@"yyyy-MM-dd" hour:0 minute:0 second:0]);
        //NSLog(@"结束时间=======%@",[GYDateTools dateString:self.currentDateString withDateFormatter:@"yyyy-MM-dd" hour:23 minute:59 second:59]);
        [GYJournal getJournalList:begin endTime:end current:[NSString stringWithFormat:@"%d",self.page] pageSize:@"10" success:^(NSArray *journals, NSInteger totalItems) {
            self.totalItems = totalItems;
            
            [self.journalTableView.mj_footer endRefreshing];
            
            [self.journalists addObjectsFromArray:journals];
            [self.journalTableView reloadData];
        } error:^(NSError *error) {
            
        }];
    }else {
        
        [self.journalTableView.mj_footer endRefreshing];
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"没有新的数据!"];
    }
}

#pragma mark -- UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.journalists.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYJournalTableViewCell *cell = [GYJournalTableViewCell cellWithTbaleView:tableView];
    cell.model = self.journalists[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击cell....");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYJournalTableViewCell *cell = [GYJournalTableViewCell cellWithTbaleView:tableView];
    
    return [cell cellHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 5)];
    backGroundView.backgroundColor = [UIColor whiteColor];
    
    return backGroundView;
}



#pragma mark -- 懒加载

- (UITableView *)journalTableView {
    if (!_journalTableView) {
        _journalTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _journalTableView.delegate = self;
        _journalTableView.dataSource = self;
        _journalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        UIView *backGroundView = [[UIView alloc] init];
        backGroundView.backgroundColor = [UIColor clearColor];
        _journalTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    }
    
    return _journalTableView;
}




#pragma mark -- 蓝架子啊

- (NSMutableArray *)journalists {
    if (!_journalists) {
        _journalists = [NSMutableArray array];
    }
    
    return _journalists;
}





@end
